<template>
  <div class="app-container hk-flex-column">
    <header class="hk-flex">
      <div class="hk-flex left">
        <img src="@/assets/images/tj-icon-04.png" alt>
        <div class="total-area">
          <p class="font-size16 total-title">全部工单</p>
          <span class="font-c-blue">{{ countObj.unitNum }}</span>
        </div>
      </div>

      <div v-if="countObj.eligibility.length" class="tab-list tab-bg hk-flex">
        <div class="hk-flex dealing">
          <div class="icon hk-flex-column">
            <div class="font-28 safe">48</div>
            <p>处理中</p>
          </div>
          <div class="hk-flex-column flex-1">
            <div v-for="(i,idx) in countObj.eligibility" :key="idx" class="sp-flex">
              <span>{{ i.name }}</span>
              <i>{{ i.value }}</i>
            </div>
          </div>
        </div>
        <div class="over">
          <div class="font-28">29</div>
          <p>已结束</p>
        </div>
      </div>
    </header>
    <div class="hk-flex-column mt20 tab-content">
      <search-form
        :data="queryParams"
        :field-list="filterInfo.fieldList"
        :list-type-info="dictList"
        @handleSearch="handleQuery"
        @handleReset="resetQuery"
      />
      <el-row>
        <el-button type="primary" size="mini" icon="el-icon-plus" @click="handleAdd">新增</el-button>
        <el-button size="small" @click="handleImport">导入</el-button>
        <el-button size="small" @click="handleExport">导出查询结果</el-button>
      </el-row>

      <hk-table
        :table-data="tableData"
        :table-columns="tableList"
        :selection="true"
        :show-index="true"
        :total="total"
        :loading="loading"
        :table-height="80"
        operate
        :query-params="queryParams"
        :operate-width="220"
        @handleSelectionChange="handleSelectionChange"
        @changePage="getDataList"
      >
        <template #expiryDate="scope">
          <span :style="colorSel(scope.row.remainingDay )">{{ scope.row. expiryDate }}</span>
        </template>
        <!-- 操作权限 -->
        <template #operate="scope">
          <el-button size="mini" type="text" @click="handleDetail(scope.row)">查看</el-button>
          <el-button size="mini" type="text" @click="handleEdit(scope.row)">编辑</el-button>
          <el-button size="mini" type="text" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </hk-table>
      <hk-upload
        ref="import_dia"
        :url="url"
        :title="title"
        :accept="accept"
        @submit="getDataList();getCount()"
      />
    </div>
  </div>
</template>

<script>
import { pageList, count, listByCode, deleteUnit } from '@/api/specialProject/unit'
import SearchForm from '@/components/HkSearchform/index.vue'
import HkTable from '@/components/Hk-Table/index.vue'
export default {
  components: {
    SearchForm,
    HkTable
  },
  data() {
    return {
      queryParams: {
        current: 1,
        size: 10
      },
      countObj: {
        unitType: [],
        eligibility: []
      },
      ids: [],
      tableData: [],
      total: 0,
      loading: false,
      url: window.baseUrl.action + '/opUnit/upload',
      title: '导入运维单位',
      accept: '.xlsx',
      tableList: [
        { label: '流水号', param: 'unitName' },
        { label: '工单标题', param: 'unitType' },
        { label: '流程名称', param: 'operatingStatus' },
        { label: '工单状态', param: 'eligibility' },
        { label: '发起人', param: 'credentialInfo' },
        { label: '发起时间', param: 'expiryDate', type: 'slot' },
        { label: '当前步骤', param: 'projectNum' }
      ],
      filterInfo: {
        fieldList: [
          { label: '关键字', type: 'input', field: 'name', placeholder: '请输入单位名称', maxlength: 20 }
        ]
      },
      dictList: {
        unitTypeList: {},
        eligibilityList: {}
      }
    }
  },
  mounted() {
    this.getDataList()
    this.getTypes()
    this.getCount()
  },
  methods: {
    colorSel(day) {
      const color = day <= 7 ? '#FF3535' : day <= 30 ? '#deaf0e' : day <= 90 ? 'orange' : ''
      return {
        color
      }
    },
    //  查询
    handleQuery() {
      this.queryParams.current = 1
      this.getDataList()
    },
    //  重置
    resetQuery() {
      this.queryParams.current = 1
      this.queryParams.size = 10
      this.getDataList()
    },
    getDataList(val) {
      this.queryParams = { ...this.queryParams, ...val }
      this.loading = true
      pageList(this.queryParams).then(res => {
        this.loading = false
        this.tableData = res.list
        this.total = res.total
      })
    },
    getCount() {
      count().then(res => {
        this.countObj = res
      })
    },
    getTypes() {
      listByCode('unitType').then(res => {
        this.dictList.unitTypeList = res.map(i => {
          return {
            label: i.dictValue,
            value: i.dictKey
          }
        })
      })

      listByCode('eligibility').then(res => {
        this.dictList.eligibilityList = res.map(i => {
          return {
            label: i.dictValue,
            value: i.dictKey
          }
        })
      })
    },

    handleSelectionChange(obj) {
      this.ids = obj.map(i => i.id)
    },
    handleDetail(row) {
      this.$router.push(window.prefix + '/specialProject/unitDetail?showDetail=1&id=' + row.id)
    },
    handleAdd() {
      this.$router.push(window.prefix + '/specialProject/unitDetail')
    },
    handleEdit(row) {
      this.$router.push(window.prefix + '/specialProject/unitDetail?id=' + row.id)
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const id = row.id || this.ids.join(',')
      this.$modal
        .confirm('是否确认删除所选数据项？')
        .then(function() {
          return deleteUnit(id)
        })
        .then(() => {
          this.$modal.msgSuccess('删除成功')
          this.getDataList()
          this.getCount()
        })
        .catch(error => {
          console.log(error)
        })
    },
    /** 导入按钮操作 */
    handleImport() {
      this.$refs.import_dia.show()
    },

    /** 导出按钮操作 */
    handleExport() {
      this.gDownload(
        window.baseUrl.action + '/opUnit/export',
        { ids: this.ids.join(',') },
        `运维单位${new Date().getTime()}.xlsx`
      )
    }
  }
}
</script>

<style lang="scss" scoped>
img {
  width: 60px;
}
.app-container {
  background: #eef0f5 !important;
  padding: 0;
  header {
    padding: 20px;
    background: #fff;
    height: 160px;
    border-radius: 4px;

    .left {
      width: 20%;
      max-width: 250px;
      img {
        width: 100px;
      }
    }
    .total-area {
      margin-left: 5px;
    }
    .tab-bg {
      background: #f3f5fb;
      padding: 20px 0;
      border-radius: 4px;
      .dealing {
        padding: 20px;
      }
      .over {
        margin-left: 10px;
        padding: 0 30px;
        height: 100%;
        display: flex;
        flex-direction: column;
        align-items: center;
        justify-content: center;
        border-left: 1px solid #e1e2e4;
      }
    }
    .font-28 {
      font-size: 28px;
      font-weight: 600;
      &.safe {
        color: #3abf81;
      }
    }
    .icon {
      align-items: center;
      margin-right: 20px;
    }
    p {
      margin: 0;
      margin-top: 10px;
    }
    .sp-flex {
      span {
        max-width: 90%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
        position: relative;
        &::before {
          content: '';
          display: inline-block;
          margin-bottom: 2px;
          margin-right: 10px;
          width: 5px;
          height: 5px;
          background: pink;
          border-radius: 50%;
        }
      }
      i {
        font-style: normal;
        font-size: 18px;
        // color: #333333;
        line-height: 24px;
        font-weight: 500;
      }
    }
    .tab-list {
      height: 100%;
      margin-right: 20px;
      // flex: 1;
      // max-width: 360px;
    }
  }
}
.tab-content {
  flex: 1;
  overflow: auto;
  padding: 20px;
  position: relative;
  background: #fff;
}
.el-row {
  position: absolute;
  right: 20px;
  top: 50px;
}
.total-title {
  margin-bottom: 15px;
  margin-top: 6px;
  line-height: 24px;
}
.font-size16 {
  font-size: 16px;
}
.flex-1 {
  // width: calc(100% - 80px);
  justify-content: space-between;
  height: 100%;
}
.font-c-blue {
  color: #4b6eef;
  font-size: 36px;
}
.mt-20 {
  margin-top: 20px;
}
::v-deep {
  .content {
    padding-bottom: 0;
  }
}
</style>
